package com.app.service.impl;

import com.alibaba.excel.EasyExcel;
import com.app.controller.importExcel.ImportExcelListener;
import com.app.mapper.MilitaryTrainMapper;
import com.app.model.MilitaryTrainModel;
import com.app.service.MilitaryTrainService;
import com.app.util.CommonHelp;
import com.app.util.TestFileUtil;
import com.app.vo.MilitaryTrainVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class MilitaryTrainServiceImpl extends ServiceImpl<MilitaryTrainMapper, MilitaryTrainModel> implements MilitaryTrainService {
    @Autowired
    private MilitaryTrainMapper militaryTrainMapper;

    @Override
    public MilitaryTrainVo getMilitaryTrainList(Integer page, Integer limit) {
        Page<MilitaryTrainModel> page1 = new Page<>(page, limit);
        Page<MilitaryTrainModel> militaryTrainPage = militaryTrainMapper.selectPage(page1, null);
        List<MilitaryTrainModel> villageList = militaryTrainPage.getRecords();
        long count = militaryTrainPage.getTotal();

        MilitaryTrainVo militaryTrainVo = new MilitaryTrainVo();
        militaryTrainVo.setList(villageList);
        militaryTrainVo.setCount((int) count);
        return militaryTrainVo;
    }

    //导入军事训练综合评定表
    @Override
    public String importMilitaryTrain(MultipartFile file) throws IOException {
        List<String> errorList = new ArrayList<>();
        String path = TestFileUtil.getPath() + "excel.xlsx";
        File localFile = new File(path);
        file.transferTo(localFile);
        String filePath = localFile.getAbsolutePath();
        AtomicReference<Integer> row = new AtomicReference<>(new Integer(0));
        try {
            EasyExcel.read(filePath, MilitaryTrainModel.class, new ImportExcelListener<MilitaryTrainModel>(dateList -> {
                for (MilitaryTrainModel militaryTrainModel : dateList) {
                    if (militaryTrainModel.getGenderText().equals("男")) {
                        militaryTrainModel.setGender(1);
                    } else {
                        militaryTrainModel.setGender(2);
                    }
                    militaryTrainModel.setCreateTime(CommonHelp.getNowTimestamp());
                    militaryTrainMapper.insert(militaryTrainModel);
                    row.set(row.get() + 1);
                }

            }, errorList)).sheet().headRowNumber(3).doRead();
        } catch (Exception e) {
            return "Excel导入失败";
        }
        localFile.delete();
        if (row.get() == 0) {
            return "导入0条，请检查模版或者数据格式";
        }
        StringBuilder msg = new StringBuilder("成功导入" + row + "行。");
        if (errorList.size() != 0) {
            for (String error : errorList) {
                msg.append(error);
            }
        }
        return msg.toString();
    }
}
